//
//  LGMainHeartUltrasoundCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/1.
//  编辑核心检查项 - 心脏彩超

#import "LGMainHeartUltrasoundCell.h"
#import "LGMainHeartReportView.h"
#import "LGCustomTextView.h"
@interface LGMainHeartUltrasoundCell()<LGTextViewDelegate>

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)LGMainHeartReportView *reportView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)LGCustomTextView *contentTF;
/**   */
@property (nonatomic , strong)NSMutableDictionary *reportDic;
/**   */
@property (nonatomic , copy)NSString *result;

@end

@implementation LGMainHeartUltrasoundCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
        __weak typeof(self) weakSelf = self;
        self.reportView.heartReportValueChanged = ^(NSDictionary * _Nonnull dataDic) {
            weakSelf.reportDic = [NSMutableDictionary dictionaryWithDictionary:dataDic];
            NSMutableDictionary *resultDic = [NSMutableDictionary dictionaryWithDictionary:dataDic[@"d_result"]];
            [resultDic setValue:weakSelf.result forKey:@"result"];
            [weakSelf.reportDic setValue:resultDic forKey:@"d_result"];
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(mainInspectEditChanged:index:key:)]) {
                [weakSelf.delegate mainInspectEditChanged:weakSelf.reportDic index:0 key:@"report7"];
            }
        };
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.reportDic = [NSMutableDictionary dictionaryWithDictionary:dataDic];
    self.reportView.dataDic = [NSMutableDictionary dictionaryWithDictionary:dataDic];
    self.result = LGNSString(dataDic[@"d_result"][@"result"]);
    NSString *canEdit = LGNSString(dataDic[@"d_result"][@"is_editable"]);
    if([canEdit isEqualToString:@"1"]){
        self.contentTF.userInteractionEnabled = YES;
        self.contentTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
    }else{
        self.contentTF.userInteractionEnabled = NO;
        self.contentTF.layer.borderColor = [UIColor clearColor].CGColor;
    }
    self.contentTF.content = self.result;
}

-(void)textViewDidEndEditing:(NSString *)content{
    self.result = content;
    NSMutableDictionary *resultDic = [NSMutableDictionary dictionaryWithDictionary:self.reportDic[@"d_result"]];
    [resultDic setValue:self.result forKey:@"result"];
    [self.reportDic setValue:resultDic forKey:@"d_result"];
    if (self.delegate && [self.delegate respondsToSelector:@selector(mainInspectEditChanged:index:key:)]) {
        [self.delegate mainInspectEditChanged:self.reportDic index:0 key:@"report7"];
    }
}

-(void)textViewHeightChanged:(CGFloat)height{
    height = (height>viewPix(80))?height:viewPix(80);
    CGFloat contentHeight = self.contentTF.size.height;
    if(height != contentHeight){
        [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(height));
        }];
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
    }
}

-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.contentView addSubview:self.reportView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.contentTF];

    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.contentView).offset(viewPix(16));
        make.top.right.equalTo(self.contentView).offset(-viewPix(16));
    }];
    [self.reportView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.height.equalTo(@(412));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.reportView.mas_bottom).offset(viewPix(10));
        make.left.equalTo(self.baseView).offset(viewPix(16));
        make.right.equalTo(self.baseView).offset(-viewPix(16));
    }];
    [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(4));
        make.bottom.equalTo(self.contentView).offset(-viewPix(4));
        make.left.right.equalTo(self.titleLabel);
        make.height.equalTo(@(viewPix(80)));
    }];
}


-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(LGMainHeartReportView *)reportView{
    if(!_reportView){
        _reportView = [[LGMainHeartReportView alloc] initWithFrame:CGRectMake(viewPix(16), 0, Screen_W-viewPix(32), 412)];
        _reportView.backgroundColor = [UIColor whiteColor];
    }
    return _reportView;
}


-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"诊断意见：" colorString:@"#333333" textFont:LGFontWeight(13, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(LGCustomTextView *)contentTF{
    if(!_contentTF){
        _contentTF = [LGCustomTextView textViewWithPlaceHolder:@"请输入" placeHolderColor:@"#999999" placeHolderFont:LGFont(13) textColor:@"#333333" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft keyboard:UIKeyboardTypeDefault];
        _contentTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _contentTF.layer.borderWidth = 1;
        _contentTF.cornerRidus = 5;
        _contentTF.customDelegate = self;
        _contentTF.maxCount = 200;
    }
    return _contentTF;
}


@end
